<template>
  <div style="display:flex ; background-color:aqua; height: 100vh; align-items: center; justify-content: center;">
    <div style="display: flex; background-color: white; width: 50%; border-radius: 10px; overflow: hidden;">
      <div style="flex: 1;">
        <img src="@/assets/1.jpg" alt="" style="width: 100%; height: 100%;">
      </div>
      <div style="flex:1; display: flex; align-items: center; justify-content: center;">
        <el-form :model="user" style="width: 80%;" :rules="rules" ref="loginRef">
          <div style="font-size: 20px; font-weight: bold; text-align: center; margin-bottom: 20px;">欢迎登陆后台管理系统</div>
          <el-form-item prop="username">
            <el-input prefix-icon="el-icon-user" size="medium" placeholder="请输入账号" v-model="user.username"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input prefix-icon="el-icon-lock" size="medium" show-password placeholder="请输入密码"
              v-model="user.password"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" style="width: 100%;" @click="login">登陆</el-button>
          </el-form-item>
          <div style="display: flex;">
            <div style="flex:1; ">没有账号？请<span style="color: aqua; cursor: pointer;"
                @click="$router.push('/register')">注册</span></div>
            <div style="flex:1; text-align: right;"><span style="color:blue; cursor: pointer;" @click="handleForgetPass">忘记密码</span></div>
          </div>
        </el-form>
      </div>
    </div>
    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
        <el-form :model="forgetUserForm" label-width="80px" style="padding-right: 20px;">
          <el-form-item label="用户名">
            <el-input v-model="forgetUserForm.name" auto-complete="off" placeholder="请输入用户名"></el-input>
          </el-form-item>
          <el-form-item label="手机号">
            <el-input v-model="forgetUserForm.phone" auto-complete="off" placeholder="请输入手机号"></el-input>
          </el-form-item>
        </el-form>
        <div style="align-items: center; justify-content: center; display: flex;">
          <div style="display: flex; flex: 1; align-items: center; justify-content: center;"> 
            <el-button style="width: 50%;"  @click="dialogVisible = false">取 消</el-button>
          </div>
         <div style="display: flex; flex: 1; justify-content: center;align-items: center;">
            <el-button style="width: 50%;" type="primary" @click="dialogVisible = false">确 定</el-button>
         </div>
        </div>
          
        
     
    </el-dialog>

  </div>
</template>
<script>
export default {
   name:"LoginView",
   data() {
     return {
       user:{
        username:'',
        password:'',
       },
       forgetUserForm:{
        name:'',
        password:'',
       },
       dialogVisible:false,
       rules:{
        username:[
          {required:true,message:'请输入账号',trigger:'blur'} ,
        ],
        password:[
          {required:true,message:'请输入密码',trigger:'blur'} ,
        ]
       }
     }
   },
   methods: {
handleForgetPass(){
  this.forgetUserForm={}
  this.dialogVisible=true
},

    login() {
      this.$refs['loginRef'].validate((valid) => {
        if (valid) {
          this.$request.post('/login', this.user).then(res => {
            if(res.code=='1'){
              localStorage.setItem('user',JSON.stringify(res.data))
              var userInfo = JSON.parse(localStorage.getItem('user') || '{}');
              if(userInfo.role=='超级管理员'||userInfo.role=='管理员'){
                this.$router.push('/home')
                this.$message.success('登陆成功')
              }else{
                this.$message.error('权限不足')
                localStorage.removeItem('user')
              }
              
            }else{
              this.$message.error(res.msg)
            }
          })

        }

      })

    }
  }
};
</script>
<style>
</style>